/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.utility;

import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;

import com.aroundj2me.model.ProgressListener;
/**
 * For displaying running progress on the screen
 *
 */
public class ProgressForm extends Form implements Runnable, ProgressListener{
	private Gauge gauge;
	private StringItem message;
	private StringItem percent;
	private int SLEEP_INTERVAL = 1; // in ms
	private boolean progressRunning = false;
	private boolean indeterminate = true; //runs continuously
	private int maxSteps = 0;
	
	/**
	 * Default constructor
	 *
	 */
	public ProgressForm(){
		super("loading...");
		initComponents();
	}
	
	/**
	 * Constructor
	 * @param isIndeterminate = sets the type of the progressbar to indeterminate
	 */
	public ProgressForm(boolean isIndeterminate){
		super("loading...");
		this.indeterminate = isIndeterminate;
		initComponents();
	}
	
	/**
	 * Initializes the visual components
	 *
	 */
	private void initComponents(){
		ImageItem img = new ImageItem("", ImageLoader.getImage(ImageLoader.GLOBE), ImageItem.LAYOUT_CENTER, null);
		message = new StringItem("loading...", null);
		message.setLayout(StringItem.LAYOUT_NEWLINE_AFTER);
		message.setLayout(StringItem.LAYOUT_CENTER);
		percent = new StringItem("0 %", null);
		percent.setLayout(StringItem.LAYOUT_NEWLINE_AFTER);
		gauge = new Gauge(null, false, 50, 0);
		gauge.setLayout(Gauge.LAYOUT_NEWLINE_BEFORE);
		gauge.setLayout(Gauge.LAYOUT_CENTER);
		this.append(img);
		this.append(message);
		this.append(gauge);
	}
	
	/**
	 * If the progressType is not "indeterminate" this method can
	 * be used to define the number of maximum steps that will be displayed
	 * @param maxSteps
	 */
	public void initialize(int maxSteps){
		gauge.setMaxValue(1000);
		this.maxSteps = maxSteps;
		gauge.setValue(0);
		this.insert(2, percent);
	}
	
	/**
	 * Increments the progressbar according to the maxSteps and
	 * maximum size of the progressbar
	 * This is only used if the progressbar is not set as
	 * "indeterminate"
	 */
	public void incrementProgress() {
		int increment = gauge.getMaxValue()/maxSteps;
		gauge.setValue(gauge.getValue() + increment);
		percent.setLabel((gauge.getValue() * 100/gauge.getMaxValue() + " %"));
	}
	
	/**
	 * Starts the progressbar with the default
	 * message
	 *
	 */
	public void startProgress(){
		gauge.setValue(0);
		progressRunning = true;
		Thread t = new Thread(this);
		t.start();
	}
	
	/**
	 * Starts the progressbar and visualizes the
	 * given message
	 * @param progressMessage = message to display while loading
	 */
	public void startProgress(String progressMessage){
		gauge.setValue(0);
		message.setLabel(progressMessage);
		startProgress();
	}
	
	/**
	 * Stops the progressbar and the thread which is
	 * incrementing it
	 *
	 */
	public void stopProgress() {
		progressRunning = false;
	}
	
	/**
	 * Run method of the thread which increments the progressbar if
	 * set as "indeterminate"
	 */
	public void run() {
        int val = gauge.getValue();
        Thread thread = Thread.currentThread();
        try {
            while(progressRunning){
            	if(indeterminate){
	                thread.sleep(SLEEP_INTERVAL);
	                gauge.setValue(val++);
	                if(val >= gauge.getMaxValue()){
	                	gauge.setValue(gauge.getMaxValue());
	                	thread.sleep(SLEEP_INTERVAL*2);
	                	val = 0;
	                }
            	}
            }
        }
        catch( InterruptedException e ){
        }
	}
	
	/*
	 * Getter and Setter
	 */
	
	public void isIndeterminate(boolean arg){
		this.indeterminate = arg;
	}
}
